package com.block.test.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.block.common.model.vo.ResultInfoVO;
import com.block.common.util.HttpReturnUtil;
import com.block.test.factory.ReqValidateFactory;
import com.block.test.service.IValidateService;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 参数校验拦截器
 */
public class Param2ValidateInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = (String) request.getAttribute("uri");
        JSONObject transData = (JSONObject) request.getAttribute("tranData");

        ResultInfoVO resultInfoVO = null;

        IValidateService validateService = ReqValidateFactory.getValidateService(uri);
        if (validateService == null) {
            return true;
        }

        resultInfoVO = validateService.handleProcess2(request, transData);
        if (resultInfoVO == null) {
            return true;
        }
        HttpReturnUtil.returnJson(response, resultInfoVO);
        return false;
    }

}
